
#include "iostream"
#include <bitset>
#include <vector>
#include "f_extern.h"

using namespace std;

int make_dir(string dir_name, int base_block)
{
	char name[10];
	int num_next = 0;
	int i,j;
	int num_chld;
	int cur_block_size,last_block_size;
	int tmp_size;
	header head;
	header new_dir;
	FILE *ptr;
	FILE *fd; 
	string chld_path;
	
	
	sprintf(name,"./.%d",base_block);
	string path = root_dir; path.append(name);
	fd = fopen(path.data(),"rb");
	fread(&head,sizeof(header),1,fd);
	fclose(fd);
	num_next = head.data_block;	
	tmp_size = head.size;
	do
	{
		sprintf(name,"./.%d",num_next);
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb");
		last_block_size = head.size;
		for(j=0;j<head.size && j<block_size-(sizeof(int)); j+=sizeof(int))
		{

			fread(&num_chld,sizeof(int),1,fd);
			sprintf(name,"./.%d",num_chld);
			chld_path = root_dir; chld_path.append(name);
			
			ptr = fopen(chld_path.data(),"rb");
			fread(&new_dir,sizeof(header),1,ptr);
			
			fclose(ptr);
			
			if(strcmp(new_dir.name,dir_name.data())==0 && new_dir.type ==0)
			{
				fclose(fd);
				return num_chld;
			}
			else
			{
				//
			}
		}
		head.size -= (block_size-sizeof(int));
		fseek(fd,-sizeof(int),SEEK_END);
		fread(&num_next,sizeof(int),1,fd);
		fclose(fd);
	}while(num_next!=0);
	
	
	if(get_free_block() <3) 
	{
#ifdef PRINT_MSG
		printf("[mkdir]Not enough free block \n");
#endif	
		return -1;
	}
	
	cur_block_size = get_free_block();

	if(last_block_size+sizeof(int)<block_size-sizeof(int))
	{
		fd = fopen(path.data(),"rb+");
		fseek(fd,last_block_size,SEEK_SET);
		fwrite(&cur_block_size,sizeof(int),1,fd);
		fclose(fd);		
	}
	else
	{
		last_block_size = get_free_block();
		fd = fopen(path.data(),"rb+");
		fseek(fd,-sizeof(int),SEEK_END);
		fwrite(&last_block_size,1,sizeof(int),fd);
		fclose(fd);
		
		sprintf(name,"./.%d",num_chld);
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb+");
		fwrite(&cur_block_size,sizeof(int),1,fd);
		fseek(fd,-sizeof(int),SEEK_END);
		last_block_size = 0;
		fwrite(&last_block_size,sizeof(int),1,fd);
		fclose(fd);
		head.size_in_block+=1;
	}
	sprintf(name,"./.%d",cur_block_size);
	path = root_dir; path.append(name);
	ptr = fopen(path.data(),"rb+");
	strcpy(new_dir.name, dir_name.data());
	new_dir.data_block = get_free_block();
	new_dir.size = 0;
	new_dir.size_in_block = 2;
	new_dir.type = 0;
	set_time(&new_dir);
	fwrite(&new_dir,1,sizeof(header),ptr);
	fclose(ptr);
	
	sprintf(name,"./.%d",new_dir.data_block);
	path = root_dir; path.append(name);
	ptr = fopen(path.data(),"rb+");
	fseek(ptr,-sizeof(int),SEEK_END);
	last_block_size = 0;
	fwrite(&last_block_size,1,sizeof(int),fd);
	fclose(ptr);
	
	head.size=tmp_size+sizeof(int);
	set_time(&head);
	sprintf(name,"./.%d",base_block);
	path = root_dir; path.append(name);
	ptr = fopen(path.data(),"rb+");
	fwrite(&head,1,sizeof(header),ptr);
	fclose(ptr);
			
	return cur_block_size;
}

int main(int argc, char* argv[])
{	
	char* _f_ = "[mkdir]";
	int next_dir_num = 0;
	int i;
	FILE* config = NULL; 
	FILE* block = NULL;
	string file_name;
	
	header dir;
	string dir_path;
	
		
	if (argc != 3){
#ifdef PRINT_MSG
		printf("%s: Use mkdir root path\n",_f_); 
#endif
		return 2;
	}
	
		root_dir = argv[1];
		make_build_map(1);
		
		dir_path = argv[2];
		parse_path(dir_path);
		
		next_dir_num = 1;
		for( i = 0;i<dir_set.size();i++)
		{
		
			next_dir_num = make_dir(dir_set[i],next_dir_num);
			if(next_dir_num<0) 
			{
				return 1;
			}
		}
	return 0;
}


